package com.yami.shop.mystery.multishop.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.mystery.common.model.ActivitySubject;
import com.yami.shop.mystery.common.service.ActivitySubjectService;
import com.yami.shop.security.common.util.AuthUserContext;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/activity/activitySubject")
@Tag(name = "活动专题接口")
public class ActivitySubjectController {

    @Autowired
    private ActivitySubjectService activitySubjectService;

    @GetMapping("/list")
    @Operation(summary = "活动专题分页列表")
    public ServerResponseEntity<IPage<ActivitySubject>> pageList(ActivitySubject vo, PageParam<ActivitySubject> pageDTO) {
        QueryWrapper<ActivitySubject> activitySubjectQueryWrapper = new QueryWrapper<>();
        activitySubjectQueryWrapper.eq("shop_id", AuthUserContext.get().getShopId());
        activitySubjectQueryWrapper.like(ObjectUtil.isNotEmpty(vo.getName()), "name", vo.getName());
        activitySubjectQueryWrapper.orderByDesc("update_time");
        IPage<ActivitySubject> page = activitySubjectService.page(pageDTO, activitySubjectQueryWrapper);
        return ServerResponseEntity.success(page);
    }

    @PostMapping("/save")
    @Operation(summary = "新增活动专题")
    public ServerResponseEntity<Void> save(@RequestBody ActivitySubject dto) {
        dto.setShopId(AuthUserContext.get().getShopId());
        activitySubjectService.save(dto);
        activitySubjectService.createQrcode(dto.getId(), AuthUserContext.get().getShopId(), dto.getIsContainFree(), dto.getActivityId());
        return ServerResponseEntity.success();
    }

    @PostMapping("/update")
    @Operation(summary = "修改活动专题")
    public ServerResponseEntity<Void> update(@RequestBody ActivitySubject dto) {
        activitySubjectService.updateById(dto);
        activitySubjectService.createQrcode(dto.getId(), AuthUserContext.get().getShopId(), dto.getIsContainFree(), dto.getActivityId());
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除活动专题")
    public ServerResponseEntity<Void> deleteBrand(@RequestParam Long id) {
        activitySubjectService.removeById(id);
        return ServerResponseEntity.success();
    }

    @GetMapping("/info/{id}")
    @Operation(summary = "获取活动专题详细信息")
    @Parameter(name = "id", description = "活动专题主键", required = true)
    public ServerResponseEntity<ActivitySubject> info(@PathVariable("id") Long id) {
        ActivitySubject activitySubject = activitySubjectService.getById(id);
        return ServerResponseEntity.success(activitySubject);
    }

    @GetMapping("/getList")
    @Operation(summary = "获取活动列表")
    public ServerResponseEntity<List<ActivitySubject>> getList(){
        QueryWrapper<ActivitySubject> activitySubjectQueryWrapper = new QueryWrapper<>();
        activitySubjectQueryWrapper.select("id","name");
        activitySubjectQueryWrapper.orderByDesc("create_time");
        List<ActivitySubject> list = activitySubjectService.list(activitySubjectQueryWrapper);
        return ServerResponseEntity.success(list);
    }

}
